package com.asm.service;

import com.asm.dto.PageResult;
import com.asm.entity.sys.District;
import com.asm.repository.DistrictRepository;
import com.asm.util.SnowflakeIdWorker;
import com.asm.vo.sys.DistrictVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 功能描述
 *
 * @param * @param null
 * @author Tanzujie
 * @date 2019-07-10
 * @return
 */
@Service
public class DistrictService {
    @Autowired
    private DistrictRepository districtRepository;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 获取省市区列表
     *
     * @param page
     * @return
     */
    private List<DistrictVo> getDistrictVoList(Page<District> page) {
        List<DistrictVo> list = new ArrayList<>();
        for (District District : page.getContent()) {
            DistrictVo districtVo = new DistrictVo();
            BeanUtils.copyProperties(District, districtVo);
            list.add(districtVo);
        }
        return list;
    }

    /**
     * 分页查询所有省市区
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageResult<DistrictVo> findAllDistricts(Integer pageNumber, Integer pageSize) {
        Page<District> page = districtRepository.findAll(PageRequest.of(pageNumber, pageSize));
        return new PageResult<DistrictVo>(pageNumber, pageSize, page.getTotalPages(), getDistrictVoList(page));
    }

    /**
     * 根据id查询省市区
     *
     * @param id
     * @return
     */
    public DistrictVo findDistrictById(Long id) {
        District District = districtRepository.findById(id).get();
        DistrictVo districtVo = new DistrictVo();
        BeanUtils.copyProperties(District, districtVo);
        return districtVo;
    }

    /**
     * 根据省区分页查询省市区
     *
     * @param province
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageResult<DistrictVo> findDistrictByProvince(String province, Integer pageNumber, Integer pageSize) {
        Pageable page = PageRequest.of(pageNumber, pageSize);
        Page<District> districts = districtRepository.findByProvince(province, page);
        return new PageResult<DistrictVo>(pageNumber, pageSize, districts.getTotalPages(), getDistrictVoList(districts));
    }

    /**
     * 根据城市分页查询省市区
     *
     * @param city
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageResult<DistrictVo> findDistrictByCity(String city, Integer pageNumber, Integer pageSize) {
        Pageable page = PageRequest.of(pageNumber, pageSize);
        Page<District> districts = districtRepository.findByCity(city, page);
        return new PageResult<DistrictVo>(pageNumber, pageSize, districts.getTotalPages(), getDistrictVoList(districts));
    }

    /**
     * 添加省市区
     *
     * @param districtVo
     */
    @Transactional(rollbackFor = Exception.class)
    public void addDistrict(DistrictVo districtVo) {
        District District = new District();
        BeanUtils.copyProperties(districtVo, District);
        District.setId(snowflakeIdWorker.nextId());
        District.setCreateBy(1L);
        District.setCreateAt(new Date());
        District.setUpdateBy(1L);
        District.setUpdateAt(new Date());
        districtRepository.save(District);
    }

    /**
     * 更新省市区
     *
     * @param districtVo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateDistrict(DistrictVo districtVo) {
        District District = districtRepository.findById(districtVo.getId()).get();
        BeanUtils.copyProperties(districtVo, District);
        District.setUpdateBy(1L);
        District.setUpdateAt(new Date());
        districtRepository.save(District);
    }

    /**
     * 根据Id删除省市区
     *
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteDistrict(Long id) {
        districtRepository.deleteById(id);
    }
}
